package com.blog.service.impl;

import com.blog.mapper.UserMapper;
import com.blog.pojo.User;
import com.blog.service.UserService;
import com.blog.util.DateUtil;
import com.blog.util.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author JiaFuJun
 * @version v1.0
 * @date 2020-05-13
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void save(User record) {
        if (record.getId() == null) {
            record.setCreateTime(DateUtil.getStrTime());
            record.setPassword(MD5Util.MD5(record.getPassword()));
            userMapper.insert(record);
        } else {
            record.setPassword(null);
            userMapper.update(record);
        }
    }

    @Override
    public void update(User record) {
        userMapper.update(record);
    }

    @Override
    public void del(Object id) {
        userMapper.del(id);
    }


    @Override
    public User selectByPrimaryKey(Object id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> getList(User record) {
        return userMapper.selectByRecord(record);
    }

    @Override
    public PageInfo<User> pageList(User record, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<User> list = userMapper.selectByRecord(record);
        return new PageInfo<>(list);
    }

    @Override
    public User getByName(String userName) {
        return userMapper.getByName(userName);
    }

}